package com.qqt.csr.common.utils;

import com.qqt.csr.common.exception.BaseException;
import com.qqt.csr.common.exception.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * 业务断言
 *
 * @auther:
 * @date: 2024-05-17 19:13
 */
@Slf4j
public class ServiceAssert {

    /**
     * 断言为真
     *
     * @param expression
     * @param be
     * @param warnLog    需要打印警告日志
     */
    public static void isTrue(boolean expression, BaseException be, String warnLog) {
        if (!expression) {
            if (StringUtils.isNotBlank(warnLog)) {
                log.warn(warnLog);
            }
            throw be;
        }
    }

    /**
     * 断言为真
     *
     * @param expression
     * @param be
     */
    public static void isTrue(boolean expression, BaseException be) {
        isTrue(expression, be, null);
    }

    /**
     * 断言为真
     *
     * @param expression
     * @param code
     * @param message
     */
    public static void isTrue(boolean expression, Integer code, String message) {
        isTrue(expression, new BaseException(code, message));
    }

    /**
     * 断言为真
     *
     * @param expression
     * @param code
     * @param message
     */
    public static void isTrue(boolean expression, Integer code, String message, String warnLog) {
        isTrue(expression, new BaseException(code, message), warnLog);
    }

    /**
     * 断言为真
     *
     * @param expression
     * @param statusCode
     */
    public static void isTrue(boolean expression, StatusCode statusCode) {
        isTrue(expression, new BaseException(statusCode.getCode(), statusCode.getMessage()));
    }

    /**
     * 断言为真
     *
     * @param expression
     * @param statusCode
     */
    public static void isTrue(boolean expression, StatusCode statusCode, String warnLog) {
        isTrue(expression, new BaseException(statusCode.getCode(), statusCode.getMessage()), warnLog);
    }

    /**
     * 断言不为空
     *
     * @param obj
     * @param be
     * @param warnLog 需要打印警告日志
     */
    public static void notNull(Object obj, BaseException be, String warnLog) {
        if (obj == null) {
            if (StringUtils.isNotBlank(warnLog)) {
                log.warn(warnLog);
            }
            throw be;
        }
    }

    /**
     * 断言不为空
     *
     * @param obj
     * @param be
     */
    public static void notNull(Object obj, BaseException be) {
        notNull(obj, be, null);
    }

    /**
     * 断言不为空
     *
     * @param obj
     * @param code
     * @param message
     */
    public static void notNull(Object obj, Integer code, String message) {
        notNull(obj, new BaseException(code, message));
    }

    /**
     * 断言不为空
     *
     * @param obj
     * @param code
     * @param message
     */
    public static void notNull(Object obj, Integer code, String message, String warnLog) {
        notNull(obj, new BaseException(code, message), warnLog);
    }

    /**
     * 断言不为空
     *
     * @param obj
     * @param statusCode
     */
    public static void notNull(Object obj, StatusCode statusCode) {
        notNull(obj, new BaseException(statusCode.getCode(), statusCode.getMessage()));
    }

    /**
     * 断言不为空
     *
     * @param obj
     * @param statusCode
     */
    public static void notNull(Object obj, StatusCode statusCode, String warnLog) {
        notNull(obj, new BaseException(statusCode.getCode(), statusCode.getMessage()), warnLog);
    }
}
